Appdelegate'in altına bunu yapıştır;
enum Actions:String{
case increment = "INCREMENT_ACTION"
case decrement = "DECREMENT_ACTION"
case reset = "RESET_ACTION"
}
var categoryID:String {
get{
return "COUNTER_CATEGORY"
}
}
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Todo: Check if settings are already registered
registerNotification()
return true
}
// Register notification settings
func registerNotification() {
// 1. Create the actions **************************************************
// increment Action
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = Actions.increment.rawValue
incrementAction.title = "Bir artır"
incrementAction.activationMode = UIUserNotificationActivationMode.Background
incrementAction.authenticationRequired = true
incrementAction.destructive = false
// decrement Action
let decrementAction = UIMutableUserNotificationAction()
decrementAction.identifier = Actions.decrement.rawValue
decrementAction.title = "Bir eksilt"
decrementAction.activationMode = UIUserNotificationActivationMode.Background
decrementAction.authenticationRequired = true
decrementAction.destructive = false
// reset Action
let resetAction = UIMutableUserNotificationAction()
resetAction.identifier = Actions.reset.rawValue
resetAction.title = "Sıfırla"
resetAction.activationMode = UIUserNotificationActivationMode.Foreground
// NOT USED resetAction.authenticationRequired = true
resetAction.destructive = true
// 2. Create the category ***********************************************
// Category
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = categoryID
// A. Set actions for the default context
counterCategory.setActions([incrementAction, decrementAction, resetAction],
forContext: UIUserNotificationActionContext.Default)
// B. Set actions for the minimal context
counterCategory.setActions([incrementAction, decrementAction],
forContext: UIUserNotificationActionContext.Minimal)
// 3. Notification Registration *****************************************
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
// Schedule the Notifications with repeat
func scheduleNotification() {
//UIApplication.sharedApplication().cancelAllLocalNotifications()
// Schedule the notification ********************************************
if UIApplication.sharedApplication().scheduledLocalNotifications!.count == 0 {
let notification = UILocalNotification()
notification.alertBody = "Sayacı sıfırlayınız"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate()
notification.category = categoryID
notification.repeatInterval = NSCalendarUnit.Minute
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
// MARK: Application Delegate
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
scheduleNotification()
}
func application(application: UIApplication,
handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification,
completionHandler: () -> Void) {
// Handle notification action *****************************************
if notification.category == categoryID {
let action:Actions = Actions(rawValue: identifier!)!
let counter = Counter();
switch action{
case Actions.increment:
counter++
case Actions.decrement:
counter--
case Actions.reset:
counter.currentTotal = 0
}
}
completionHandler()
}
daha sonra ;
yeni bir sınıf aç sayaç ve counter neyse bunu yapıştır;
class Counter {
var UD_KEY:String {
return "TotalCount"
}
var currentTotal:Int {
get{
let userDefault = NSUserDefaults.standardUserDefaults()
return userDefault.integerForKey(UD_KEY)
}
set(newValue){
let userDefault = NSUserDefaults.standardUserDefaults()
userDefault.setInteger(newValue, forKey: UD_KEY)
userDefault.synchronize()
// Post a notification to let observers know about the new value
let notificationCenter = NSNotificationCenter.defaultCenter()
let notification = NSNotification(name: "COUNTER_UPDATED", object: nil)
notificationCenter.postNotification(notification)
}
}
}
postfix func ++ (counter:Counter){
counter.currentTotal += 1
}
postfix func -- (counter:Counter){
counter.currentTotal -= 1
}
daha sonrada view'in altına bunu koy:
let counter = Counter()
@IBOutlet weak var totalLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// set the label value
totalLabel.text = "\(counter.currentTotal)"
// Register for Counter notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.updateUI(_:)), name: "COUNTER_UPDATED", object: nil)
}
// MARK: UI Functions
@IBAction func resetCount() {
counter.currentTotal = 0
}
@IBAction func incrementTotal(){
counter++
}
@IBAction func decrementTotal(){
counter--
}
func updateUI(notification:NSNotification){
totalLabel.text = "\(counter.currentTotal)"
}